package com.ruoyi.framework.web.service;

import com.ruoyi.biz.domain.BizAddress;
import com.ruoyi.biz.domain.BizUser;
import com.ruoyi.biz.service.IBizAddressService;
import com.ruoyi.biz.service.IBizUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.biz.BizRegistBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.exception.user.CaptchaExpireException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 注册校验方法
 *
 * @author ruoyi
 */
@Component
public class BizRegisterService {
    @Autowired
    private IBizUserService userService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IBizAddressService addressService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 注册
     */
    public String registerBizUser(BizRegistBody registerBody) {
        String msg = "";
        String fullName = registerBody.getFirstName() + " " + registerBody.getLastName();

        boolean captchaOnOff = configService.selectCaptchaOnOff();
        // 验证码开关
        if (captchaOnOff) {
            validateCaptcha(registerBody.getCode(), registerBody.getUuid());
        }

        BizUser condition = new BizUser();
        condition.setEmail(registerBody.getEmail());
        List<BizUser> list = userService.selectBizUserList(condition);

        if (list.size() > 0) {
            msg = "email existed!";
        } else {
            BizUser bizUser = new BizUser();
            BeanUtils.copyProperties(registerBody, bizUser);
            bizUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword()));
            int result = userService.insertBizUser(bizUser);
            if (result == 0) {
                msg = "注册失败,请联系系统管理人员";
            } else {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(fullName, Constants.REGISTER,
                        MessageUtils.message("user.register.success")));
            }
        }
        return msg;
    }


    /**
     * 注册
     */
    public boolean registerBizUser1(BizRegistBody registerBody) {
        String fullName = registerBody.getFirstName() + " " + registerBody.getLastName();
        BizUser bizUser = new BizUser();
        BeanUtils.copyProperties(registerBody, bizUser);
        bizUser.setCreateTime(DateUtils.getNowDate());
        userService.insertBizUser(bizUser);
        BizAddress bizAddress = new BizAddress();
        bizAddress.setDefaultFlag(1);
        bizAddress.setState(registerBody.getState());
        bizAddress.setUserId(bizUser.getUserId());
        bizAddress.setAddressee(fullName);
        bizAddress.setPostAddress(bizUser.getPostAddress());
        bizAddress.setMobilePhone(bizUser.getPhone());
        bizAddress.setAddressee(bizUser.getFirstName() + bizUser.getLastName());
        addressService.insertBizAddress(bizAddress);
        return true;
    }

    /**
     * 校验验证码
     *
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public void validateCaptcha(String code, String uuid) {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null) {
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha)) {
            throw new CaptchaException();
        }
    }
}
